package com.dashu.authority.servie.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.common.MessageCode;
import com.dashu.authority.entity.AuthenticationInfo;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.AuthenticationInfoMapper;
import com.dashu.authority.servie.AuthenticationInfoService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.github.pagehelper.PageHelper;

@Service
public class AuthenticationInfoServiceImpl extends BaseServiceImpl<AuthenticationInfo>
		implements AuthenticationInfoService {
	private static Long EFFECTIVE_TIME = (long) (2 * 60 * 60 * 1000);

	@Autowired
	private AuthenticationInfoMapper infoMapper;

	@Override
	public List<AuthenticationInfo> queryAllByExample(AuthenticationInfo entity, int pageNum, int pageSize)
			throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		// params.put("status", true);
		// params.put("isDelete", true);
		// if (entity!=null) {
		// if (entity.getId()!=null) {
		// params.put("id", entity.getId());
		// }
		// if (entity.getLoginId()!=null && entity.getLoginId()!="") {
		// params.put("loginId", entity.getLoginId());
		// }
		// if (entity.getStatus()!=null) {
		// params.put("status", entity.getStatus());
		// }
		// if (entity.getIsDelete()!=null) {
		// params.put("isDelete", entity.getIsDelete());
		// }
		// if (entity.getUserName()!=null && entity.getUserName()!="") {
		// params.put("UserName", entity.getUserName());
		// }
		// }
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		return infoMapper.queryAllByExample(params);
	}

	@Override
	public String insertEntity(String loginId, String communityCode) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户账号", loginId);
		AuthenticationInfo info = new AuthenticationInfo();
		info.setLoginId(loginId);
		info.setCommunityCode(communityCode);
		long l = System.currentTimeMillis()+EFFECTIVE_TIME;
		info.setExpiry(String.valueOf(l));// 时间加长2小时
		info.setLoginTime(new Date());
		String token = getUUID();
		info.setToken(token);
		int ret = infoMapper.insertEntity(info);
		if (ret <= 0) {
			throw new AuthorityException("2100001");
		}
		return token;
	}

	private static String getUUID() {
		return java.util.UUID.randomUUID().toString().replace("-", "");
	}

	@Override
	public Map<String, Object> deleteByIds(Integer[] ids) throws AuthorityException {
		if (ids == null || ids.length == 0) {
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(), "用户id数组:");
		}
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("ids", ids);
		int ret = infoMapper.deleteByIds(params);
		if (ret > 0) {
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		} else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}

	@Override
	public AuthenticationInfo queryInfoByToken(String token) throws AuthorityException {
		AuthorityServiceUtil.checkParam("用户token", token);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("token", token);
		List<AuthenticationInfo> list = infoMapper.queryAllByExample(params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int updateInfoByToken(Integer id) throws AuthorityException {
		AuthorityServiceUtil.checkParam("ID", id);
		Map<Object, Object> params = new HashMap<Object, Object>();
		params.put("id", id);
		long l = System.currentTimeMillis()+EFFECTIVE_TIME;
		params.put("expiry", String.valueOf(l)); ;
		int ret = infoMapper.updateTime(params);
		return ret;
	}

}
